package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.CustomerMainOriginal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2016/4/22.
 */
public class CustomerMultipleAdapter extends ArrayAdapter<CustomerMainOriginal.rCustomerMain> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-08-04 14:52:10 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  Map<Integer, Boolean> isCheckMap = new HashMap<Integer, Boolean>();

  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvCustomerNameCustomerListAdapter;
    public final TextView tvCustomerDicritCustomerListAdapter;
    private final TextView tvIsDraft;
    private final CheckBox cbCustomer;

    private ViewHolder(LinearLayout rootView, TextView tvCustomerNameCustomerListAdapter,
        TextView tvCustomerDicritCustomerListAdapter, TextView tvIsDraft, CheckBox cbCustomer) {
      this.rootView = rootView;
      this.tvCustomerNameCustomerListAdapter = tvCustomerNameCustomerListAdapter;
      this.tvCustomerDicritCustomerListAdapter = tvCustomerDicritCustomerListAdapter;
      this.tvIsDraft = tvIsDraft;
      this.cbCustomer = cbCustomer;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvCustomerNameCustomerListAdapter =
          (TextView) rootView.findViewById(R.id.tv_customerName_customerListAdapter);
      TextView tvCustomerDicritCustomerListAdapter =
          (TextView) rootView.findViewById(R.id.tv_customerDicrit_customerListAdapter);
      TextView tvIsDraft = (TextView) rootView.findViewById(R.id.tv_isDraft_customerListAdapter);
      CheckBox cbCustomer = (CheckBox) rootView.findViewById(R.id.cb_customerList);
      return new ViewHolder(rootView, tvCustomerNameCustomerListAdapter,
          tvCustomerDicritCustomerListAdapter, tvIsDraft, cbCustomer);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      //            View widget = mInflater.inflate( R.layout.adapter_customer_list, parent, false);
      View view = mInflater.inflate(R.layout.adapter_customer_multiple, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    CustomerMainOriginal.rCustomerMain item = getItem(position);

    //Bind your data to the views here
    vh.tvCustomerNameCustomerListAdapter.setText(item.getCustomerName());
    vh.tvCustomerDicritCustomerListAdapter.setText(item.getDistrictDesc());

    String dataStatus = item.getDataStatus();
    //草稿
    if (dataStatus != null && dataStatus.equalsIgnoreCase("new")) {
      vh.tvIsDraft.setText(R.string.draft);
      vh.tvIsDraft.setTextColor(mContext.getResources().getColor(R.color.white));
      vh.tvIsDraft.setBackgroundColor(mContext.getResources().getColor(R.color.red));
    }
    //已提交
    else if (dataStatus != null && dataStatus.equalsIgnoreCase("submit")) {
      vh.tvIsDraft.setText(R.string.already_submit);
      vh.tvIsDraft.setTextColor(mContext.getResources().getColor(R.color.white));
      vh.tvIsDraft.setBackgroundColor(mContext.getResources().getColor(R.color.blue_textColor));
    }
    //已审核
    else if (dataStatus != null && dataStatus.equalsIgnoreCase("publish")) {
      vh.tvIsDraft.setText(R.string.hasAudited);
      vh.tvIsDraft.setTextColor(mContext.getResources().getColor(R.color.black));
      vh.tvIsDraft.setBackgroundColor(mContext.getResources().getColor(R.color.gray_dark));
    }
    //返回修改
    else if (dataStatus != null && dataStatus.equalsIgnoreCase("newback")) {
      vh.tvIsDraft.setText(R.string.removeCheck);
      vh.tvIsDraft.setTextColor(mContext.getResources().getColor(R.color.black));
      vh.tvIsDraft.setBackgroundColor(mContext.getResources().getColor(R.color.yelloy_textColor));
    }

    vh.cbCustomer.setTag(position);
    // 找到需要选中的条目
    if (isCheckMap != null && isCheckMap.containsKey(position)) {
      vh.cbCustomer.setChecked(isCheckMap.get(position));
    } else {
      vh.cbCustomer.setChecked(false);
    }

    vh.cbCustomer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      private int mCheckPosition;

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mCheckPosition = Integer.parseInt(buttonView.getTag().toString());
        if (isChecked) {
          // 将选中的放入hashmap中
          isCheckMap.put(mCheckPosition, isChecked);
        } else {
          // 取消选中的则剔除
          isCheckMap.remove(mCheckPosition);
        }
      }
    });

    return vh.rootView;
  }

  public boolean isCheckedItem(int position) {
    if (isCheckMap != null && isCheckMap.containsKey(position)) {
      return true;
    } else {
      return false;
    }
  }

  private LayoutInflater mInflater;
  private Context mContext;

  // Constructors
  public CustomerMultipleAdapter(Context context,
      List<CustomerMainOriginal.rCustomerMain> customerDetails) {
    super(context, 0, customerDetails);
    this.mInflater = LayoutInflater.from(context);
    this.mContext = context;
  }

  public CustomerMultipleAdapter(Context context, CustomerMainOriginal.rCustomerMain[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}



